//
//  ViewController.m
//  MultipleThreadDemo
//
//  Created by shuqiong on 16/5/17.
//  Copyright © 2016年 shuqiong. All rights reserved.
//

#import "ViewController.h"
#import "MyOperation.h"
#import "OperationManager.h"

@interface ViewController ()

- (IBAction)clicked:(id)sender;

@property (nonatomic, strong) MyOperation *operation;
@property (nonatomic, strong) NSOperationQueue *operationQueue;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    /*
     同步任务会阻塞当前线程，然后把 Block 中的任务放到指定的队列中执行，只有等到 Block 中的任务完成后才会让当前线程继续往下运行。
     那么这里的步骤就是：打印完第一句后，dispatch_sync 立即阻塞当前的主线程，然后把 Block 中的任务放到 main_queue 中，可是 main_queue 中的任务会被取出来放到主线程中执行，但主线程这个时候已经被阻塞了，所以 Block 中的任务就不能完成，它不完成，dispatch_sync 就会一直阻塞主线程，这就是死锁现象。导致主线程一直卡死。
     
     文／伯恩的遗产（简书作者）
     原文链接：http://www.jianshu.com/p/0b0d9b1f1f19
     著作权归作者所有，转载请联系作者获得授权，并标注“简书作者”。
     */
/*
    NSLog(@"之前 %@", [NSThread currentThread]);
    dispatch_sync(dispatch_get_main_queue(), ^{
        NSLog(@"sync %@", [NSThread currentThread]);
    });
    NSLog(@"之后 %@", [NSThread currentThread]);
   */
    
/*
    dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL);
    NSLog(@"之前 %@", [NSThread currentThread]);
    dispatch_async(queue, ^{
        NSLog(@"sync 之前 %@", [NSThread currentThread]);
        dispatch_sync(queue, ^{
            NSLog(@"sync %@", [NSThread currentThread]);
        });
        NSLog(@"sync 之后 %@", [NSThread currentThread]);
    });
    NSLog(@"之后 %@", [NSThread currentThread]);
 */
    
/*
    //队列组
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_group_async(group, queue, ^{
        for (int i = 0; i < 3; i ++) {
            NSLog(@"group-01 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_group_async(group, dispatch_get_main_queue(), ^{
        for (int i = 0; i < 8; i++) {
            NSLog(@"group-02 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_group_async(group, queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"group-03 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"完成 %@", [NSThread currentThread]);
    });
    dispatch_group_notify(group, queue, ^{
        NSLog(@"完成 %@", [NSThread currentThread]);
    });
 */
    
/*
//    添加依赖
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片 %@", [NSThread currentThread]);
        [NSThread sleepForTimeInterval:1.0];
    }];
 
    NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"打水印 %@", [NSThread currentThread]);
        [NSThread sleepForTimeInterval:1.0];
    }];
    
    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"上传图片 %@", [NSThread currentThread]);
        [NSThread sleepForTimeInterval:1.0];
    }];
    
    [operation2 addDependency:operation1];
    [operation3 addDependency:operation2];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperations:@[operation1, operation3, operation2] waitUntilFinished:NO];
 */
    
/*
    dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
    dispatch_source_set_timer(source, 0, 1, 0);
 */
    
/*
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        self.operation = [[MyOperation alloc] init];
        self.operation.operationDes = @"operation1";
        [self.operation start];
    });
 */
    
    self.operationQueue = [[NSOperationQueue alloc] init];
    self.operationQueue.maxConcurrentOperationCount = 3;
    for (int i = 0; i < 5; i ++) {
        MyOperation *op = [[MyOperation alloc] init];
        op.operationDes = [NSString stringWithFormat:@"ope******: %d", i];
        [self.operationQueue addOperation:op];
    }
 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)clicked:(id)sender {
    NSLog(@"clicked");
//    [self.operation cancel];
    [self.operationQueue cancelAllOperations];
}

@end
